package com.gdcp.controller.backend;

import com.gdcp.beans.Role;
import com.gdcp.beans.User;
import com.gdcp.common.ResponseModel;
import com.gdcp.config.RequestHolder;
import com.gdcp.service.RoleService;
import com.gdcp.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Controller
@RequestMapping("/render/userManager")
@RolesAllowed({"ROLE_SHOP_ADMIN","ROLE_ADMIN"})
public class UserController_backend {

    @Autowired
    ResponseModel responseModel;
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    CommonCode commonCode;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(int pageSize, int pageNumber, String sortName, String sortOrder){
        Map<String,Object> model=commonCode.getList(pageSize, pageNumber, sortName, sortOrder);

        User user= (User) RequestHolder.getSession("user");
        for (Role role: user.getRoles()){
            if (role.getName().equals("ROLE_ADMIN")){
                return userService.findAll(model);
            }
        }

        Integer shop_id=user.getShop_id();
        model.put("shop_id",shop_id);
        return userService.findPageBreakByCondition(model, shop_id);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> remove(Integer[] ids){
        if (ids==null){
            return responseModel.setModel(false,"请至少选择一条记录");
        }
        return userService.removeByPrimaryKey(ids);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(User user){
        /*if (user.getShop_id()==null){
            User user1= (User) RequestHolder.getSession("user");
            user.setShop_id(user1.getShop_id());
        }*/
        return userService.register(user);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(User user){
        return userService.updateInformation(user);
    }

    @RequestMapping(value = "/get/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> get(@PathVariable Integer id){
        User user=userService.getInformation(id);
        if (user==null){
            return responseModel.setModel(false,"获取失败");
        }

        return responseModel.setModel(true,user);
    }

    @RequestMapping(value = "/search",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> search(String searchText){
        String str = "["+searchText+"]";
        JSONArray json= JSONArray.fromObject(str);
        Map<String, Object> param=new HashMap<>();

        for(int i=0;i<json.size();i++){
            JSONObject jsonOne = json.getJSONObject(i);
            //System.out.println("jsonOne="+jsonOne);
            param.put("username", jsonOne.getString("username"));
            param.put("id", jsonOne.getString("searchid"));
            param.put("email", jsonOne.getString("email"));
            param.put("shop_id", jsonOne.getString("shopid"));
        }

        User user= (User) RequestHolder.getSession("user");
        for (Role role: user.getRoles()){
            if (role.getName().equals("ROLE_ADMIN")){
                return userService.searchInformation(param);
            }
        }

        param.put("shop_id",user.getShop_id());
        if (user.getShop_id()==0){
            return responseModel.setModel(false,"信息错误");
        }

        return userService.searchInformationByShopId(param,user.getShop_id());
    }
}
